package com.qymerge.manage.system.service.impl;

import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.bean.DataTableRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.utils.DBUtils;
import com.qymerge.manage.core.utils.Env;
import com.qymerge.manage.system.dao.IDatabackDao;
import com.qymerge.manage.system.dto.DatabackDto;
import com.qymerge.manage.system.ro.DatabackRO;
import com.qymerge.manage.system.service.IDatabackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *<p>Title: DatabackServiceImpl</p>
 *<p>Description: 数据库备份</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/18
 *@Time 23:38
 */
@Service
public class DatabackServiceImpl implements IDatabackService {

    @Autowired
    private IDatabackDao databackDao;

    /**
     * 查询
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public DataTableRO list(DatabackDto ro, PageRequest pageRequest) {
        List list = databackDao.list(ro, pageRequest);
        DataTableRO dataTableRO = new DataTableRO();
        int allCount = databackDao.total(ro);
        int pageSize = pageRequest.getSize();
        dataTableRO.setAll_count(allCount);
        dataTableRO.setList(list);
        dataTableRO.setCurrent_page(pageRequest.getPage());
        dataTableRO.setPage_size(pageSize);
        int allPage = (allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1);
        dataTableRO.setAll_page(allPage);
        return dataTableRO;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public DatabackRO detail(Long id) {
        return databackDao.findOneById(id);
    }


    /**
     * 后台管理系统-用户管理-新增
     *
     * @param ro
     * @return
     */
    @Override
    public ActionResultRO save(DatabackRO ro) {
        int rn = 0;
        if (ro.getId() == 0) {
            String file_name = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".sql";
            boolean databaseTool = DBUtils.exportDatabaseTool(Env.dbback_hostip, Env.dbback_port, Env.dbback_username, Env.dbback_password, Env.dbback_save_path, file_name, Env.dbback_databasename);
            if (databaseTool) {
                ro.setFiledata(file_name);
                ro.setCreatedate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            }
            rn = databackDao.install(ro);
            return new ActionResultRO(rn, "备份成功", "备份失败");
        } else {
            rn = databackDao.update(ro);
            return new ActionResultRO(rn, "修改成功","修改失败");
        }

    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public ActionResultRO delete(Long[] ids) {
        int rn = databackDao.removeList(Arrays.asList(ids));
        return new ActionResultRO(true, "删除" + rn +"条成功", "删除" + rn +"条失败");
    }


}
